from database import Database

class Proxy:
	def __init__(self, proxy_id):
		self.db = Database()
		self.id = proxy_id
		self.load_data()
		self.loaded = False

	def load_data(self):
		# Get proxy basic attributes
		sql = "SELECT INET_NTOA(ip) as ip, port, user, password, unix_timestamp(checked) as checked, active FROM proxys WHERE idproxy = '" + str(self.id) + "'"
		res = self.db.execute(sql)
		if res.__len__() > 0:
			res = res[0]
			self.ip = res['ip']
			self.port = res['port']
			self.user = res['user']
			self.password = res['password']
			self.checked = res['checked']
			self.active = res['active']
			

	def load_properties(self):
		self.loaded = True
		# Get proxy extended attributes
		sql = "SELECT * FROM proxy_property " + \
			"INNER JOIN properties ON proxy_property.idc_property = properties.idproperty " + \
			"WHERE idc_proxy = '" + str(self.id) + "'"
		res = self.db.execute(sql)
		self.properties = []
		for property in res:
			m = ( property['property'], property['value'] )
			self.properties.append(m)

	def get_property(self, pname):
		# Default data
		if pname == "id":
			return self.id
		if pname == "ip":
			return self.ip
		if pname == "port":
			return self.port
		if pname == "user":
			return self.user
		if pname == "password":
			return self.password
		if pname == "checked":
			return self.checked
		if pname == "active":
			return self.active

		# Extended data	
		if not self.loaded:
			self.load_properties()
		for prop in self.properties:
			if prop[0] == pname:
				return prop[1]

	def set_property(self, pname, value):
		pname = str(pname)
		if pname == "":
			return False
		value = str(value)
		if pname == "user" or pname == "password" or pname == "checked" or pname == "active":
			if pname == "user" or pname == "password":
				sql = "UPDATE proxys SET " + pname + "='" + value + "' WHERE idproxy='" + str(self.id) + "'"
			if pname == "checked":
				sql = "UPDATE proxys SET checked=NOW() WHERE idproxy='" + str(self.id) + "'"
			if pname == "active":
				if value == "True":
					sql = "UPDATE proxys SET active=1 WHERE idproxy='" + str(self.id) + "'"
				else:
					sql = "UPDATE proxys SET active=0 WHERE idproxy='" + str(self.id) + "'"
			res = self.db.execute(sql)
			return True

		sql = "SELECT idproperty FROM properties WHERE property='" + pname + "'"
		res = self.db.execute(sql)
		if res:
			property_id = str(res[0]['idproperty'])
			sql = "SELECT value FROM proxy_property WHERE idc_proxy='" + str(self.id) + "' AND idc_property='" + property_id + "'"
			res = self.db.execute(sql)
			if res:
				sql = "UPDATE proxy_property SET value='" + value + "' WHERE idc_proxy='" + str(self.id) + "' AND idc_property='" + property_id + "'"
			else:
				sql = "INSERT INTO proxy_property (idc_proxy, idc_property, value) VALUES ('" + str(self.id) + "', '" + property_id + "', '" + value + "')"
			res = self.db.execute(sql)
		else:
			sql = "INSERT INTO properties (property) VALUES ('" + pname + "')"
			res = self.db.execute(sql)
			self.set_property(pname, value)
